﻿namespace Nello.Data
{
    using System;
    using System.Data;

    public class NPagerUtil
    {
        public static DataTable GetPageData(string tablename, string keyname, int pageindex, int pagesize)
        {
            return GetPageData(tablename, keyname, pageindex, pagesize, " 1=1 ");
        }

        public static DataTable GetPageData(string tablename, string keyname, int pageindex, int pagesize, string where)
        {
            string sql = "";
            string sqlx = "";
            if (pageindex == 0)
            {
                sql = "select top [pagesize] * from [tablename] where [where] order by [keyname]";
                return LinkerUtil.ExecuteDataTable(sql.Replace("[pagesize]", pagesize.ToString()).Replace("[tablename]", tablename).Replace("[keyname]", keyname).Replace("[where]", where));
            }
            int pagesizes = pageindex * pagesize;
            sqlx = "select max([keyname]) from [tablename] where [keyname] in (select top [pagesizes] [keyname] from [tablename] where [where] order by [keyname])";
            sql = "select top [pagesize] * from [tablename] where [keyname]>([sqlx]) and [where] order by [keyname]";
            return LinkerUtil.ExecuteDataTable(sql.Replace("[sqlx]", sqlx).Replace("[tablename]", tablename).Replace("[keyname]", keyname).Replace("[pagesize]", pagesize.ToString()).Replace("[pagesizes]", pagesizes.ToString()).Replace("[where]", where));
        }

        public static DataTable GetPageDataByProc(string tablename, string keyname, int pageindex, int pagesize)
        {
            return GetPageDataByProc(tablename, keyname, pageindex, pagesize, "");
        }

        public static DataTable GetPageDataByProc(string tablename, string keyname, int pageindex, int pagesize, string where)
        {
            Linker linker = new Linker();
            linker.CommandType = CommandType.StoredProcedure;
            linker.AddParameter("@tblName", tablename);
            linker.AddParameter("@fldName", keyname);
            linker.AddParameter(DbType.Int32, "@PageSize", pagesize);
            linker.AddParameter(DbType.Int32, "@PageIndex", pageindex);
            if (where != "")
            {
                linker.AddParameter("@where", where);
            }
            linker.Open();
            DataTable dt = linker.ExecuteDataTable("GetPageRecord");
            linker.Close();
            return dt;
        }

        public static int GetTotalCount(string tablename)
        {
            return GetTotalCount(tablename, "");
        }

        public static int GetTotalCount(string tablename, string where)
        {
            string sql = "select count(*) from [tablename] ";
            if (where != "")
            {
                sql = (sql + " where [where] ").Replace("[where]", where);
            }
            return int.Parse(LinkerUtil.ExecuteScalar(sql.Replace("[tablename]", tablename)).ToString());
        }
    }
}

